<?php
  header ("Content-type: image/png");
  header ("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
  header ("Pragma: no-cache");
  putenv ('GDFONTPATH=' . realpath('.'));
  
  require_once('./config.php');
  require_once('./includes/funcs_img.php');
  require_once('./includes/funcs_wow.php');
  
  $conn = @mysql_connect(DBHOST.':'.DBPORT, DBUSER, DBPASS);
  if(!$conn) empty_image('SQL Connection Error ...');
  if(!@mysql_select_db(DB_CHR)) empty_image('SQL Database Error ...');
  if(!@mysql_select_db(DATABASE)) empty_image('SQL Database Error ...');
  @mysql_query('SELECT * FROM `'.TAB_SRC.'` LIMIT 1;') or empty_image('SQL Table Error ...');
  @mysql_query('SELECT * FROM `'.TAB_DAT.'` LIMIT 1;') or empty_image('SQL Table Error ...');
  
  
  $sign_id = intval($_GET['id']);
  
  $sql_return = @mysql_query('SELECT * FROM `'.TAB_DAT.'` WHERE `id`='.$sign_id.' LIMIT 1');
  $sign_data = @mysql_fetch_assoc($sql_return);
  if($sign_data['id'] == null)
    empty_image('Wrong Image ID ...');
    
  $char = explode('|', $sign_data['f1']);
  if((get_usr_id($char[1]) === false) && ($sign_data['type'] != 0))
    empty_image('Character does not exist ...');
  unset($char);
  
  $sql_return = @mysql_query('SELECT * FROM `'.TAB_SRC.'` WHERE `id`='.$sign_data['src_id'].' AND `type`="SIGNATURE" LIMIT 1');
  $bg_data = @mysql_fetch_assoc($sql_return);
  
  if($bg_data['id'] != null) {
    $sign_img = load_img('./'.$bg_data['path']);
    
    if((!$sign_img) || (!check_parameters_db_data($sign_data)) || (!check_parameters_db_src($bg_data)) || ($sign_data['f_used'] > $bg_data['fields']))
      empty_image();
    
    for($i=1; $i<=$sign_data['f_used']; $i++) {
      $field = explode('|', $sign_data['f'.$i]);
      
      if($i == 1)
        $user_name = $field[1];
      
      if($field[2] != $lastfont) {
        $ret = @mysql_query('SELECT `id`,`path` FROM `'.TAB_SRC.'` WHERE `id`='.intval($field[2]).' AND `type`="FONT" LIMIT 1');
        $dat = @mysql_fetch_assoc($ret);
        $lastfont = $dat['id'];
      }
      
      if($sign_data['type'] == 0) {
        $text = $field[1];
      } elseif($sign_data['type'] == 1) {
        if($i == 1)
          $text = $user_name;
        else
          if($field[0] == 0)
            $text = $field[1];
          else
            $text = SELECT(PATCH, $user_name, $field[0]);
      } elseif($sign_data['type'] == 2) {
        if($i == 1)
          $text = SELECT(PATCH, $user_name, 0);
        else
          if($field[0] == 0)
            $text = $field[1];
          else
            $text = SELECT(PATCH, $user_name, $field[0]);
      }
      
      if($field[4] == 0)
        puttext($sign_img, $bg_data['f'.$i.'_x'], $bg_data['f'.$i.'_y'], $bg_data['f'.$i.'_s'], $field[3], './'.$dat['path'], $text);
      else
        puttextshadow($sign_img, $bg_data['f'.$i.'_x'], $bg_data['f'.$i.'_y'], $field[6], $field[7], $bg_data['f'.$i.'_s'], $field[3], $field[5], './'.$dat['path'], $text);
    }
    
    imagepng($sign_img);
    imagedestroy($sign_img);
  }
  else
    empty_image();  

  @mysql_close($conn);
?>